//引入第三方包
const gulp = require('gulp')
const cssmin = require('gulp-cssmin')
const sass = require('gulp-sass')(require('sass'))
const babel = require('gulp-babel')
const uglify = require('gulp-uglify')
const htmlmin = require('gulp-htmlmin')
const clean = require('gulp-clean')
const webserver = require('gulp-webserver')
const {
    series
} = require('gulp')
//创建一个任务
//css打包
gulp.task('css', function () {
    return gulp.src('./src/css/**').pipe(cssmin()).pipe(gulp.dest('./out/css/'))
})
//sass打包
gulp.task('sass', function () {
    return gulp.src('/.src/sass/**')
        .pipe(sass())
        .pipe(cssmin())
        .pipe(gulp.dest('./out/css/'))
})
//js打包
gulp.task('js', function () {
    return gulp
        .src('./src/js/**')
        .pipe(babel({
            presets: ['es2015']
        }))
        .pipe(uglify())
        .pipe(gulp.dest('./out/js'))
})
//html打包
gulp.task('html', function () {
    return gulp
        .src('./src/page/**')
        .pipe(htmlmin({
            removeAttributeQuotes: true,
            collapseWhitespace: true
        }))
        .pipe(gulp.dest('./out/pages'))
})
//复制static
gulp.task('static', function () {
    return gulp
        .src('./src/static/**')
        .pipe(gulp.dest('./out/static'))
})
//清除out文件夹
gulp.task('clean', function () {
    return gulp
        .src('./out', {
            allowEmpty: true
        })
        .pipe(clean())
})
//创建一个webserver任务，自动打开浏览器
gulp.task('webserver', function () {
    return gulp
        .src('./out')
        .pipe(webserver({
            host: 'localhost',
            port: 3000,
            livereload: true,
            open: './pages/index.html'
        }))
})

//创建一个watch任务，监听自动刷新修改的内容
gulp.task('watch', function () {
    gulp.watch('./src/css/**', gulp.parallel('css'))
    gulp.watch('./src/sass/**', gulp.parallel('sass'))
    gulp.watch('./src/js/**', gulp.parallel('js'))
    gulp.watch('./src/page/**', gulp.parallel('html'))
    gulp.watch('./src/static/**', gulp.parallel('static'))

})
//定义一个默认任务
exports.default = gulp.series('clean',
    gulp.parallel('css', 'sass', 'js', 'html', 'static'),
    series('webserver', 'watch')
)